package com.sgb.kjwl.viewmodel.packers;


import android.text.TextUtils;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.FindPeopleEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.WorkYear;
import com.sgb.kjwl.model.entity.WorkerLevel;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.packers.PackersPeopleFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
*@author liwei
*@date 2020/10/19
*description:
*/
public class FragFindPeopleModel extends BaseViewModel {
    private PackersPeopleFragment mFragment;
    public HomeRepertory repertory;
    private int page = 1;
    private int limit = 10;
    private Map<String,Object> map;

    public FragFindPeopleModel(PackersPeopleFragment fragment, HomeRepertory repertory) {
        this.mFragment = fragment;
        this.repertory = repertory;
    }

    public void getNewList(String cityNo, String type, String workType, String workLevel, String age,boolean isLoaderMore){
        map=new HashMap<>();
        page = isLoaderMore ? page + 1 : 1;
        map.put("provinceNo", repertory.getAreaCode());
        map.put("limit", limit);
        map.put("page", page);
        if(!TextUtils.isEmpty(cityNo)){
            map.put("cityNo",cityNo);
        }
        map.put("age",age);
        map.put("workLevel",workLevel);

        if (TextUtils.isEmpty(type)){
            map.put("type", "0");
        }else {
            map.put("type",type);
        }

        if(!TextUtils.isEmpty(workType)){
            map.put("workType",workType);
        }
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getPackerPeopleList(map).enqueue(new Callback<BaseEntity<List<FindPeopleEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<FindPeopleEntity>>> call, Response<BaseEntity<List<FindPeopleEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    mFragment.showList(response.body().getData(), isLoaderMore, response.body().getData() == null || response.body().getData().size() < limit);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<FindPeopleEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void getClassLeaderList(String cityNo, String workingAgeRange, String workTypeCode, String workLevel, String age, boolean isLoaderMore) {
        map = new HashMap<>();
        page = isLoaderMore ? page + 1 : 1;
        map.put("areacode", repertory.getAreaCode());
        map.put("provinceNo", repertory.getAreaCode());
        map.put("limit", limit);
        map.put("page", page);
        map.put("age",age);
        map.put("workLevel",workLevel);
        map.put("workTypeCode", workTypeCode);
        if (!TextUtils.isEmpty(cityNo)) {
            map.put("cityNo", cityNo);
        }
        map.put("workingAgeRange", workingAgeRange);
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getClassLeaderList(map).enqueue(new Callback<BaseEntity<List<FindPeopleEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<FindPeopleEntity>>> call, Response<BaseEntity<List<FindPeopleEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    mFragment.showList(response.body().getData(), isLoaderMore, response.body().getData() == null || response.body().getData().size() < limit);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<FindPeopleEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    /**
     * 获取城市
     */
    public void getAddressModel() {
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getAreaSecondAndThird(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> data = response.body().getData();
                    if (data == null || data.size() == 0) {
                        data = new ArrayList<>();
                    }
                    mFragment.setAreaData(data);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("workerDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + id + "&isdele=0";
                    mFragment.getUrl(url);
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getClassLeaderH5Url(String no) {
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("leaderDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no;
                    mFragment.getUrl(url);
                } else {
                    MToast.showToast(mFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //工种
    public void getWorkType() {
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getTeamPriceAPI().getWorkType().
                enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {

                        DialogHelper.dismissProgressDialog();
                        if (response.body() != null && response.body().getState() != null && response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            mFragment.setWorkTypeData(response.body().getData());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    public void getWorkYear() {
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getWorkYearList().enqueue(new Callback<BaseEntity<List<WorkYear>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<WorkYear>>> call, Response<BaseEntity<List<WorkYear>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && response.body().getState() != null && response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<WorkYear> workYears = response.body().getData();
                    List<PickSelectEntity> data = new ArrayList<>();
                    if (workYears != null && workYears.size() > 0) {
                        for (WorkYear workYear : workYears) {
                            PickSelectEntity entity = new PickSelectEntity();
                            entity.setNo(String.valueOf(workYear.getType()));
                            entity.setProjectscale(workYear.getDesc());
                            data.add(entity);
                        }
                    }
                    mFragment.setWorkYearData(data);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<WorkYear>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getWorkerLevel() {
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getWorkerLevel().
                enqueue(new Callback<BaseEntity<List<WorkerLevel>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<WorkerLevel>>> call, Response<BaseEntity<List<WorkerLevel>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() != null && response.body().getState() != null && response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            List<WorkerLevel> workYears = response.body().getData();
                            List<PickSelectEntity> data = new ArrayList<>();
                            if (workYears != null && workYears.size() > 0) {
                                for (WorkerLevel workerLevel : workYears) {
                                    PickSelectEntity entity = new PickSelectEntity();
                                    entity.setNo(String.valueOf(workerLevel.getType()));
                                    entity.setProjectscale(workerLevel.getDesc());
                                    data.add(entity);
                                }
                            }
                            mFragment.setWorkerLevelData(data);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<WorkerLevel>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    public void getWorkerYear() {
        DialogHelper.showProgressDialog(mFragment.getContext(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getWorkerYear().
                enqueue(new Callback<BaseEntity<List<WorkYear>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<WorkYear>>> call, Response<BaseEntity<List<WorkYear>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() != null && response.body().getState() != null && response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            List<WorkYear> workYears = response.body().getData();
                            List<PickSelectEntity> data = new ArrayList<>();
                            if (workYears != null && workYears.size() > 0) {
                                for (WorkYear workYear : workYears) {
                                    PickSelectEntity entity = new PickSelectEntity();
                                    entity.setNo(String.valueOf(workYear.getType()));
                                    entity.setProjectscale(workYear.getDesc());
                                    data.add(entity);
                                }
                            }
                            mFragment.setWorkerYearData(data);
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<WorkYear>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

}
